package net.kesumu.general.android.EasyStorage;

import java.io.FileInputStream;
import java.io.FileOutputStream;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;



public class EasyStorage {
    private final Context context;
    private final SharedPreferences sharedPref;
    
    public EasyStorage(Context context, String preference_file_key) {
        this.context = context;
        this.sharedPref = this.context.getSharedPreferences(preference_file_key, Context.MODE_PRIVATE);
    }

    
    public void putString(String key, String value) {
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putString(key, value);
        editor.commit();
    }
    
    public String getString(String key) {
        String value = sharedPref.getString(key, null);
        return value;
    }
    
    public void saveImage(Bitmap b,String name,String extension){
        name=name+"."+extension;
        FileOutputStream out;
        try {
            out = context.openFileOutput(name, Context.MODE_PRIVATE);
            b.compress(Bitmap.CompressFormat.JPEG, 100, out);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public Bitmap getImageBitmap(String name,String extension){
        name=name+"."+extension;  
        try{
            FileInputStream fis = context.openFileInput(name);
                Bitmap b = BitmapFactory.decodeStream(fis);
                fis.close();
                return b;
        } catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
